package com.way.filemanager.fragment;

import android.app.Activity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.way.filemanager.R;
import com.way.filemanager.providers.FilelistManager;
import com.way.filemanager.util.AndroidHelper;
import com.way.filemanager.util.CommonIdDef;

public class CategoryListAdpter extends BaseAdapter {

    Activity mActivity;
    FilelistManager mlistManager;
    int [][] ItemSet={
             //ID,ImageID,StringId,Icon Id ,
             {CommonIdDef.CATEGORY_ITEM_ID_PICTURE,
                 R.drawable.ic_filemanager_photo_normal,
                 R.string.CategoryItemPic,
                 R.drawable.color_image},
             {CommonIdDef.CATEGORY_ITEM_ID_MUSIC,
                 R.drawable.ic_filemanager_music_normal,
                 R.string.CategoryItemMusic,
                 R.drawable.color_audio},
             {CommonIdDef.CATEGORY_ITEM_ID_VIDEO,
                 R.drawable.ic_filemanager_video_normal,
                 R.string.CategoryItemVideo,
                 R.drawable.color_video},
             {CommonIdDef.CATEGORY_ITEM_ID_PACKAGE,
                 R.drawable.ic_filemanager_apk_normal,
                 R.string.CategoryItemApk,
                 R.drawable.color_apk},
             {CommonIdDef.CATEGORY_ITEM_ID_TEXT,
                 R.drawable.ic_filemanager_file_normal,
                 R.string.CategoryItemText,
                 R.drawable.color_document},
             {CommonIdDef.CATEGORY_ITEM_ID_ZIP,
                 R.drawable.ic_filemanager_zip_normal,
                 R.string.CategoryItemZip,
                 R.drawable.color_zip},
    };
    private int[] Item={CommonIdDef.CATEGORY_ITEM_ID_PICTURE,
            CommonIdDef.CATEGORY_ITEM_ID_MUSIC,
            CommonIdDef.CATEGORY_ITEM_ID_VIDEO,
            CommonIdDef.CATEGORY_ITEM_ID_PACKAGE,
            CommonIdDef.CATEGORY_ITEM_ID_TEXT,
            CommonIdDef.CATEGORY_ITEM_ID_ZIP
    };

    public CategoryListAdpter(Activity main,FilelistManager listMgr ) {
        mActivity = main;
        mlistManager = listMgr;
    }

    @Override
    public int getCount() {

            return Item.length;
    }

    @Override
    public Object getItem(int position) {
            return position;
    }

    @Override
    public long getItemId(int id) {
            return id;
    }

    class ViewHolder {
        TextView text;
        ImageView icon;
        TextView num;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                LayoutInflater mInflater = LayoutInflater.from(mActivity);
                convertView = mInflater.inflate(R.layout.category_mainview_item, null);
                // Creates a ViewHolder and store references to the two children views
                // we want to bind data to.
                holder = new ViewHolder();
                holder.text = (TextView) convertView.findViewById(R.id.category_item_text);
                holder.icon = (ImageView) convertView.findViewById(R.id.category_item_image);
                holder.num= (TextView) convertView.findViewById(R.id.category_item_num);
                convertView.setTag(holder);
                //bug108551
                AbsListView.LayoutParams param = new AbsListView.LayoutParams(
                        AbsListView.LayoutParams.MATCH_PARENT,
                        (int)AndroidHelper.convertDpToPixel(mActivity,127));
                convertView.setLayoutParams(param);
            } else {
                // Get the ViewHolder back to get fast access to the TextView
                // and the ImageView.
                holder = (ViewHolder) convertView.getTag();
            }
            int id=Item[position];
            int text_res = 0;
            int image_res = 0;
            for(int []itemT:ItemSet)
            {
                if(itemT[0] == id)
                {
                    text_res = itemT[2];
                    image_res = itemT[1];
                    break;
                }
            }
            holder.icon.setImageResource(image_res);
            //holder.text.setText(text_res);
            if(mlistManager != null)
            {
                String text = mActivity.getResources().getString(text_res);
                holder.text.setText(text);
                /*mActivity.getResources().getQuantityString(
                        R.plurals.numberOfCategoryItem,
                        mlistManager.GetFileCount(id) ,
                        mlistManager.GetFileCount(id)
                        )*/
                holder.num.setText(String.valueOf(mlistManager.GetFileCount(id)));//(CommonFunc.FormetFileSize(mlistManager.GetTypeSize(id)));
                //holder.num.setCompoundDrawablesWithIntrinsicBounds(color_res,0,0,0);
            }
            else
                holder.num.setText(R.string.CategoryNumNone);
            convertView.setId(id);
            //ThumbnailUtils.getThumbnail(path);
            return convertView;
    }

}
